package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.example.myapp.chat.ChatAdapter;
import com.example.myapp.chat.ChatBean;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class RobotActivity extends AppCompatActivity {
    public static final int MSG_OK=1;
    private ListView listView;
    private ChatAdapter adapter;
    private List<ChatBean> chatBeanList;
    private EditText edit_send;
    private Button bt_chat;
    private String sendMsg;
    private MHandler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_robot);
        chatBeanList=new ArrayList<ChatBean>();
        mHandler=new MHandler();
        listView =(ListView)findViewById(R.id.list_chat);
        edit_send=(EditText)findViewById(R.id.edit_chat);
        bt_chat=(Button)findViewById(R.id.btn_chat);
        adapter=new ChatAdapter(chatBeanList,this);
        listView.setAdapter(adapter);
        bt_chat.setOnClickListener(view ->{
            sendData();
        });
    }
    private void sendData(){
        sendMsg=edit_send.getText().toString();
        if (!TextUtils.isEmpty(sendMsg)){
            edit_send.setText("");
            sendMsg=sendMsg.replaceAll("","").replaceAll("\n","").trim();
            ChatBean chatBean=new ChatBean();
            chatBean.setMessage(sendMsg);
            chatBeanList.add(chatBean);
            adapter.notifyDataSetChanged();
            getDataFromServer();
        }
    }
    private void getDataFromServer(){
        OkHttpClient okHttpClient =new OkHttpClient();
        Request request =new Request.Builder().url(
                "http://api.qingyunke.com/api.php?key=free&appid=0&msg="+sendMsg).build();
        Call call=okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
            }
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String res=response.body().string();
                Message msg=new Message();
                msg.what=MSG_OK;
                msg.obj=res;
                mHandler.sendMessage(msg);
            }
        });
    }
    private void showData(String message){
        ChatBean chatBean=new ChatBean();
        chatBean.setMessage(message);
        chatBean.setState(ChatBean.RECEIVE);
        chatBeanList.add(chatBean);
        adapter.notifyDataSetChanged();
    }
    private void parseData(String JsonData){
        try {
            JSONObject object=new JSONObject(JsonData);
            String content=object.getString("content");
            showData(content);
        }catch (JSONException e){
            e.printStackTrace();
        }
    }
    class MHandler extends Handler {
        @Override
        public void dispatchMessage(@NotNull Message msg){
            super.dispatchMessage(msg);
            switch (msg.what){
                case MSG_OK:
                    if (msg.obj!= null){
                        String vlResult=(String)msg.obj;
                        parseData(vlResult);
                    }
            }
        }
    }

}